/**
 * 
 */
package org.ssi.web.struts;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.StringConverter;
import org.apache.struts2.ServletActionContext;
import org.ssi.dao.BaseDao;
import org.ssi.web.struts.bean.UploadFile;
import org.ssi.web.struts.converter.DateConverter;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @author 
 *
 */
public abstract class BaseAction extends ActionSupport implements Preparable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected List list;
	protected Object model;
	protected BaseDao dao;
	protected Long id;
	
	static {
		registConverter();
	}

	public static void registConverter() {
		ConvertUtils.register(new BooleanConverter(), Boolean.class);
		ConvertUtils.register(new StringConverter(), String.class);
		ConvertUtils.register(new ShortConverter(null), Short.class);
		ConvertUtils.register(new IntegerConverter(null), Integer.class);
		ConvertUtils.register(new LongConverter(null), Long.class);
		ConvertUtils.register(new FloatConverter(null), Float.class);
		ConvertUtils.register(new DoubleConverter(null), Double.class);
		ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
		ConvertUtils.register(new DateConverter(null), Date.class);
	}
	
	public Long getId()
	{
		return id;
	}
	
	public void setId(Long id)
	{
//		if(id != null && !"save".equals(getMethod()))
//		{
//			//System.out.println("setid==="+id);		
//			this.model = getObject(dao, id);
//		}
		this.id = id;
	}
//	
//	@SuppressWarnings("unchecked")
//	public Object getObject(BaseDao baseDao, Long id)
//	{
//		//System.out.println("base object");
//		return baseDao.selectEntity(id);
//	}

	/* （非 Javadoc）
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public Object getModel() {
		//System.out.println("model==="+(list != null ? "list" : "model"));		
		return list != null ? list : model;
	}
	
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	/**
	 * 通过ActionContent读取要执行的方法(Method)
	 * @return 执行的方法
	 */
	protected String getMethod()
	{
		return ActionContext.getContext().getActionInvocation().getProxy().getMethod();
	}
	
	//如果页面表单有上传文件，则FileUploadInterceptor会保存文件至上传目录并封装成UploadFile的List
	
	protected List<UploadFile> uploadFileList;

    public void setUploadFileList(List<UploadFile> uploadFileList) {
		this.uploadFileList = uploadFileList;
	}
    
    public List<UploadFile> getUploadFileList(){
    	return this.uploadFileList;
    }

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}


}
